<template>
  <div class="mt-[16px]">
    <XForm ref="form" :formData="info" :fieldList="fieldList" :options="formOption" :rules="rules" :disabled="disabled">
      <template #reason>
        <ReasonCascade ref="reasonCascade" :info="info" :platform_id="info.PlatformId" :firstOption="firstOption" />
      </template>
      <template #refund_amount>
        <RefundAmountInput :value="info" />
      </template>
      <!-- <template #is_generate_return>
        <el-checkbox v-model="info.is_generate_return" :true-value="1" :false-label="0"></el-checkbox>
      </template> -->
    </XForm>
  </div>
</template>

<script setup>
import XForm from "@/components/XForm/index.vue"
import ReasonCascade from "../../../ReasonCascade/index.vue"
import RefundAmountInput from "../RefundAmountInput/index.vue"
import { overallOptions, getWarehouseOptions } from "@/api/public"
import { getReasonList } from "@/views/user/refund//api/index"
const props = defineProps({
  country: { type: Array, default: () => [] },
  info: { type: Object, default: () => {} },
  disabled: { type: Boolean, default: false },
})
// import api from '@/views/user/list/add/api/index'
import api from "@/views/user/list/add/api/index"
const firstOption = ref([])
const reasonCascade = ref()
const refundReasonOpt = ref([])
const warehouseOpt = ref([])
// 选项 待优化
const getTreeList = async (PlatformId) => {
  try {
    console.log(1111111)

    // 一级退款原因
    firstOption.value = (await getReasonList({ platform_id: PlatformId || "1" })).data
      .filter((v) => v.parent_id == 0 && v.is_enable == 1)
      .map((v) => ({
        ...v,
        label: v.reason,
        value: v.id,
      }))
    // 回显二级原因
    if (props.info.RefundReason2) {
      reasonCascade.value?.getSecondaryOptions(props.info.primary_cause_id)
    }
    // 平台退款原因
    let platform = {
      1: "eBay_refund_reason",
      2: "amazon_adjustment_reason",
    }
    refundReasonOpt.value = (await overallOptions({ params: [platform[PlatformId]], option: "new" })).data[platform[PlatformId]]

    cargoHandlerOpt(props.info.CargoStatus)
  } catch (e) {
    console.log(e)
  } finally {
  }
}
const form = ref()
// 切换 货物处理   重置 退件仓库 & 自动生成退件单 & 退件追踪号
const cargoHandlerChange = async (e) => {
  props.info.ReturnWarehouseId = ""
  // props.info.is_generate_return = 1
  props.info.TrackingNumber = ""
  // 退货上架
  if (e == 2) {
    const res = await getWarehouseOptions({ status: 1 })
    warehouseOpt.value = res.data
    props.info.ReturnWarehouseId = props.info.ship_warehouse_id || ""
    props.info.TrackingNumber = props.info.real_track_no
    rules.ReturnWarehouseId[0].required = false
    rules.TrackingNumber[0].required = false
  }
  if (e == 3) {
    const res = await getWarehouseOptions({ status: 1, is_back_warehouse: 1 })
    warehouseOpt.value = res.data

    rules.ReturnWarehouseId[0].required = true
    rules.TrackingNumber[0].required = true
    // otto 默认 退件追踪号
    if (props.info.PlatformId == 3) {
      props.info.TrackingNumber = props.info?.return_number
      props.info.ReturnWarehouseId = res.data?.find((v) => v.label == "MIC德国仓")?.value
    }
  }

  // warehouseOpt.value = []
}
const cargoHandlerOpt = async (e) => {
  // 退货上架
  if (e == 2) {
    const res = await getWarehouseOptions({ status: 1 })
    warehouseOpt.value = res.data
  }
  if (e == 3) {
    const res = await getWarehouseOptions({ status: 1, is_back_warehouse: 1 })
    warehouseOpt.value = res.data
  }
}
watch(
  () => props.info.PlatformId,
  (val) => {
    if (val) {
      getTreeList(val)
    }
  }
)
const fieldList = ref([
  { type: "title", label: "退款描述" },
  {
    field: "RefundReason1",
    label: "退款原因",
    slot: "reason",
  },
  {
    field: "refund_amount",
    label: "退款金额",
    slot: "refund_amount",
  },
  { type: "br" },

  {
    field: "IsDispute",
    label: "存在纠纷",
    type: "select",
    options: {
      data: [
        { label: "否", value: 0 },
        { label: "是", value: 1 },
      ],
    },
    style: { width: "200px", marginRight: "-10px" },
  },
  {
    // field: "dispute_type",
    field: "DisputeType",
    label: "纠纷类型",
    type: "select",
    origin: "refund_dispute_type",
    style: { width: "276px" },
    show: ({ model }) => model.IsDispute === 1,
  },
  {
    // field: "is_cut_payment",
    field: "IsCutPayment",
    label: "是否扣款",
    type: "select",
    style: { width: "200px" },
    options: {
      data: [
        { label: "是", value: 1 },
        { label: "否", value: 0 },
      ],
    },
    show: ({ model }) => model.DisputeType === 3,
  },

  {
    // field: "is_upgrade",
    field: "IsUpgrade",
    label: "是否升级",
    type: "select",
    style: { width: "200px" },
    options: {
      data: [
        { label: "是", value: 1 },
        { label: "否", value: 0 },
      ],
    },
    show: ({ model }) => model.IsDispute === 1 && [2, 3].includes(model.DisputeType),
  },

  { type: "br" },
  {
    field: "CargoStatus",
    label: "货物处理",
    type: "select",
    origin: "cargo_handler",
    // options: {
    //   data: [
    //     { label: "未退回", value: "0" },
    //     { label: "退货上架", value: "1" },
    //     { label: "退货退回", value: "2" },
    //   ],
    // },
    style: { width: "276px" },
    change: cargoHandlerChange,
  },
  {
    // field: "return_warehouse_id",
    field: "ReturnWarehouseId",
    label: "退件仓库",
    type: "select",
    options: {
      data: warehouseOpt,
    },
    style: { width: "276px" },
    show: ({ model }) => [2, 3].includes(+model?.CargoStatus),
  },

  // {
  //   slot: "is_generate_return",
  //   label: "自动生成退件单",
  //   show: ({ model }) => [2, 3].includes(+model?.CargoStatus),
  // },
  {
    // field: "tracking_number",
    field: "TrackingNumber",
    label: "退件追踪号",
    style: { width: "276px" },
    show: ({ model }) => [2, 3].includes(+model?.CargoStatus),
  },
  { type: "br" },
  {
    field: "isApiRefund",
    label: "是否api退款",
    type: "select",
    options: {
      data: [
        { label: "是", value: 1 },
        { label: "否", value: 0 },
      ],
    },
    style: { width: "276px" },
    disabled: true,
    show: ({ model }) => model.PlatformId == 1,
  },
  // {
  //   field: "platform_refund_reason",
  //   label: "平台退款原因",
  //   type: "select",
  //   options: {
  //     data: refundReasonOpt,
  //   },
  //   style: { width: "276px" },
  //   show: ({ model }) => model.PlatformId == 1 && model?.is_api_refund == 1,
  // },
  {
    field: "RefundRemark",
    label: "客户退款描述",
    type: "textarea",
    style: { width: "864px" },
    autosize: { minRows: 2, maxRows: 6 },
  },
])

const rules = reactive({
  RefundReason1: [{ required: true, message: "请选择退款原因", trigger: "change" }],
  reason: [{ required: true, message: "请选择退款原因", trigger: "blur" }],
  ReqRefundPrice: [{ required: true, message: "请输入退款金额", trigger: "blur" }],
  IsDispute: [{ required: true, message: "请选择是否存在纠纷", trigger: "change" }],
  CargoStatus: [{ required: true, message: "请选择货物处理", trigger: "change" }],
  DisputeType: [{ required: true, message: "请选择纠纷类型", trigger: "blur" }],
  IsCutPayment: [{ required: true, message: "请选择是否扣款", trigger: "blur" }],
  ReturnWarehouseId: [{ required: true, message: "请选择退件仓库", trigger: "blur" }],
  TrackingNumber: [{ required: true, message: "请输入退件追踪号", trigger: "blur" }],
  platform_refund_reason: [{ required: true, message: "请选择平台退款原因", trigger: "change" }],
  RefundRemark: [{ required: true, message: "请输入客户退款描述", trigger: "blur" }],
  // is_api_refund: [{ required: true, message: "请选择是否API退款", trigger: "change" }],
  is_upgrade: [{ required: true, message: "请选择", trigger: "change" }],
})

const formOption = ref({
  inline: true,
  labelPosition: "top",
  // requireAsteriskPosition: "right",
})
defineExpose({ form, warehouseOpt })
</script>
<style lang="scss" scoped></style>
